package com.xxx.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.xxx.common.dto.Pagination;
import com.xxx.common.utils.DateUtil;
import com.xxx.model.SysMenu;

@Repository
public class SysMenuDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 保存
	 * @param sysMenu
	 * @return id
	 */
	public long save(SysMenu sysMenu){
		String sql = "insert into sys_menu(id,orderno,menuname,menuurl,parentid,icon,status,recordstatus,createtime,updatetime) values (?,?,?,?,?,?,?,1,?,?)";
		Object obj[] = {sysMenu.getId(),sysMenu.getOrderno(),sysMenu.getMenuname(),sysMenu.getMenuurl(),sysMenu.getParentid(),sysMenu.getIcon(),sysMenu.getStatus(),sysMenu.getCreatetime(),sysMenu.getUpdatetime()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return sysMenu.getId();
		}
		return 0;
	}
	
	/**
	 * 修改
	 * @param sysMenu
	 * @return id
	 */
	public long update(SysMenu sysMenu){
		String sql = "update sys_menu set orderno=?,menuname=?,menuurl=?,parentid=?,icon=?,status=?,updatetime=? where id=?";
		Object obj[] = {sysMenu.getOrderno(),sysMenu.getMenuname(),sysMenu.getMenuurl(),sysMenu.getParentid(),sysMenu.getIcon(),sysMenu.getStatus(),sysMenu.getUpdatetime(), sysMenu.getId()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return sysMenu.getId();
		}
		return 0;
	}
	
	/**
	 * 逻辑删除
	 * @param id
	 * @return true/false
	 */
	public boolean delete(long id){
		String sql = "update sys_menu set recordstatus=0,updatetime=? where id=?";
		Object obj[] = {DateUtil.dateToLong(),id};
		int ret = jdbcTemplate.update(sql, obj);
		return ret > 0;
	}
	
	/**
	 * 获取一个
	 * @param id
	 * @return SysMenu
	 */	
	public SysMenu getEntity(long id){
		String sql = "select id,orderno,menuname,menuurl,parentid,icon,status,createtime,updatetime from sys_menu where recordstatus=1 and id=?";
		final SysMenu sysMenu = new SysMenu();
		jdbcTemplate.query(sql, new Object[]{id},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				getResultSet(rs, sysMenu);
            }
		});
		return sysMenu;
	}

	/**
	 * 获取list
	 * @param whereMap
	 * @return list
	 */		
	public List<SysMenu> getList(Map<String, Object> whereMap){
		final List<SysMenu> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,orderno,menuname,menuurl,parentid,icon,status,createtime,updatetime from sys_menu where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new SysMenu()));
            }
		});
		return list;
	}

	/**
	 * 获取list,分页
	 * @param whereMap
	 * @param pagination
	 * @return list
	 */			
	public List<SysMenu> getList(Map<String, Object> whereMap,Pagination pagination){
		final List<SysMenu> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,orderno,menuname,menuurl,parentid,icon,status,createtime,updatetime from sys_menu where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		sql.append(" order by " + pagination.getOrder());
		sql.append(" limit " + pagination.getPage() + "," + pagination.getPageSize());
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new SysMenu()));
			}
		});
		return list;
	}

	/**
	 * 获取个数
	 * @param whereMap
	 * @return int
	 */		
	public int getCount(Map<String, Object> whereMap){
		StringBuffer sql = new StringBuffer("select count(1) from sys_menu where recordstatus=1");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		Integer count = jdbcTemplate.queryForObject(sql.toString(), obj, Integer.class);
		if(count != null){
			return count;
		}
		return 0;
	}
	
	/**
	 * 获取条件
	 * @param whereMap
	 * @param sql
	 * @param whereObj
	 * @return sql
	 */	
	private StringBuffer getCondition(Map<String, Object> whereMap,StringBuffer sql,List<Object> whereObj){
		if(whereMap == null){
			return sql;
		}
		if (whereMap.containsKey("orderno")) {
			int orderno = (int) whereMap.get("orderno");
			sql.append(" and orderno=?");
			whereObj.add(orderno);
		}
		if (whereMap.containsKey("menuname")) {
			String menuname = (String) whereMap.get("menuname");
			sql.append(" and menuname=?");
			whereObj.add(menuname);
		}
		if (whereMap.containsKey("menuurl")) {
			String menuurl = (String) whereMap.get("menuurl");
			sql.append(" and menuurl=?");
			whereObj.add(menuurl);
		}
		if (whereMap.containsKey("parentid")) {
			long parentid = (long) whereMap.get("parentid");
			sql.append(" and parentid=?");
			whereObj.add(parentid);
		}
		if (whereMap.containsKey("icon")) {
			String icon = (String) whereMap.get("icon");
			sql.append(" and icon=?");
			whereObj.add(icon);
		}
		if (whereMap.containsKey("status")) {
			int status = (int) whereMap.get("status");
			sql.append(" and status=?");
			whereObj.add(status);
		}
		return sql;
	}
	
	/**
	 * 获取结果集
	 * @param whereMap
	 * @param sysMenu
	 * @return sysMenu
	 */	
	private SysMenu getResultSet(ResultSet rs,SysMenu sysMenu) throws SQLException{
		sysMenu.setId(rs.getLong("id"));
		sysMenu.setOrderno(rs.getInt("orderno"));
		sysMenu.setMenuname(rs.getString("menuname"));
		sysMenu.setMenuurl(rs.getString("menuurl"));
		sysMenu.setParentid(rs.getLong("parentid"));
		sysMenu.setIcon(rs.getString("icon"));
		sysMenu.setStatus(rs.getInt("status"));
		sysMenu.setCreatetime(rs.getLong("createtime"));
		sysMenu.setUpdatetime(rs.getLong("updatetime"));
		return sysMenu;
	}
}